package servlets;

import beans.Examen;
import beans.Matiere;
import beans.Promotion;
import beans.Utilisateur;
import dao.DAOFactory;
import dao.ExamenDAO;
import dao.MatiereDAO;
import dao.PromotionDAO;
import forms.AjouterExamenForm;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet(name = "AjouterExamen", urlPatterns = {"/ajouterExamen"})
public class AjouterExamen extends HttpServlet {

   public static final String CONF_DAO_FACTORY = "daofactory";
   public static final String ATT_EXAM         = "examen";
   public static final String ATT_FORM         = "form";
   public static final String VUE              = "/WEB-INF/ajouterExamen.jsp";
   
   private ExamenDAO examenDAO;
   private MatiereDAO matiereDAO;
   private PromotionDAO promotionDAO;
   
   private List<Matiere> listeM;
   private List<Promotion> listeP;
   
    @Override
     public void init() throws ServletException {
        
        /* Récupération d'une instance de nos DAO */
        this.examenDAO = ( (DAOFactory) getServletContext().getAttribute( CONF_DAO_FACTORY ) ).getExamenDao();
        this.matiereDAO = ( (DAOFactory) getServletContext().getAttribute( CONF_DAO_FACTORY ) ).getMatiereDao();
        this.promotionDAO = ( (DAOFactory) getServletContext().getAttribute( CONF_DAO_FACTORY ) ).getPromotionDao();
        
        listeM = new ArrayList<Matiere>();
        listeP = new ArrayList<Promotion>();
    }
     
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        HttpSession session = request.getSession();
        Utilisateur utilisateur  = (Utilisateur) session.getAttribute("sessionUtilisateur");
        
        listeM = matiereDAO.matieresEnseignees(utilisateur.getId());
        /* Stockage dans l'objet request */
        request.setAttribute("listeMatieres", listeM);
        
        
        listeP = promotionDAO.promotionsPriseEnCharge(utilisateur.getId());
        request.setAttribute("listePromotions", listeP);
        
        this.getServletContext().getRequestDispatcher( VUE ).forward( request, response );
    }
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
                
        /* Préparation de l'objet formulaire */
        AjouterExamenForm form = new AjouterExamenForm( examenDAO );
 
        /* Traitement de la requête et récupération du bean en résultant */
        Examen examen = form.creerExamen( request );
        
        /* Stockage dans l'objet request */
        request.setAttribute( ATT_FORM, form );
        request.setAttribute( ATT_EXAM, examen );
        request.setAttribute("listeMatieres", listeM);
        request.setAttribute("listePromotions", listeP);
 
        /* Affichage de la page */
        this.getServletContext().getRequestDispatcher( VUE ).forward( request, response );
    }
}